package com.chenfan.common.extension.context;

import com.chenfan.common.config.Constant;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 链路上下文信息
 *
 * @author wulg
 * @date 2022-04-26
 **/
public class TraceContext {

    public static final String CURRENT_USER = "currentUser";

    public static final String APP_KEY = "app_key";

    public static final String TENANT_ID = "tenantId";

    public static final String TOKEN = Constant.AUTHORIZATION_TOKEN;

    public static final String ACTION_NAME = "action_name";
    
    public static final String SCENE_ENTRY = "scene_entry";

    private final Map<String, Object> MAP;

    public TraceContext(int initialCapacity) {
        this.MAP = new ConcurrentHashMap<>(initialCapacity);
    }

    public Object get(String key) {
        return MAP.get(key);
    }

    protected <T> void add(String key, T value) {
        MAP.put(key, value);
    }

    protected void remove(String key) {
        MAP.remove(key);
    }

    protected void clear() {
        MAP.clear();
    }

    protected void copy(TraceContext target) {
        if(Objects.isNull(target)) {
            return;
        }
        for(Map.Entry<String, Object> entry : this.MAP.entrySet()) {
            target.add(entry.getKey(), entry.getValue());
        }
    }

    /**
     * 链路场景入口
     */
     public enum SceneEntry {
        HTTP,
        JOB
    }
}
